.text
.global csp_entry
.type csp_entry, @function

#-------------------------------------------------------------------
# Entry function for entrying a task:
#  csp_entry(void *kernel_sp, void *process_sp, void *syscall_ptrs)
#-------------------------------------------------------------------
csp_entry:
	/* restore kernel stack to before fetch_task-loop */
	ori $sp, $3, 0

	/* save kernel sp */
	ori $80, $sp, 0

	/* switch to process stack */
	ori	$sp, $4, 0

	/* setup syscall-pointr */
	stqa $5, kernel_syscalls

#	ila $sp, __stack

# Calculate the default stack size (from $sp to _end)
#	ila     $0, _end
#	sf      $0, $0, $sp
#
#	fsmbi	$127, 0x0F00
#	selb	$sp, $sp, $0, $127
	


	/* set process link register to NULL */
	il	$lr, 0

	/* initialize back chain to NULL */
	stqd	$lr, 0($sp)

	/* initialize stack frame */
	stqd	$sp, -32($sp)
	ai	$sp, $sp, -32

	/* save link register in link register save area */
	stqd	$lr, 16($sp)

	/* call _init function */
	brsl	$lr, _init

	/* call the process main function */
	brsl	$lr, main

	/* restore kernel sp */
	ori $sp, $80, 0

	/* jump to main_loop */
	br csp_exit

